import {
  HttpApi
} from '../../../../model/index.js'
const httpApi = new HttpApi

Page({
  data: {
    windowHeight: 100,
    scale: 15,
    latitude: "",
    longitude: "",
    address: "",
    start: 1,
    size: 300,
    lng: "",
    lat: "",
    markers: [],
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    var login = wx.getStorageSync('login')
    
    if (options.latitude != undefined && options.latitude != "")
    that.latitude = options.latitude;
    if (options.longitude != undefined && options.longitude != "")
    that.longitude = options.longitude;
    //设置scroll-view高度
    wx.getSystemInfo({
      success: function (res) {
        that.setData({ windowHeight: res.windowHeight - 1 });
      }
    });
    

    wx.getLocation({
      type: 'wgs84',
      success: function (res) {
        //发送请求通过经纬度反查地址信息  
        var getAddressUrl = "";
        if (options.latitude == undefined || options.latitude == "") {
          getAddressUrl = "https://apis.map.qq.com/ws/geocoder/v1/?location=" + res.latitude + "," + res.longitude + "&key=OTXBZ-RNX3P-VEIDG-VJDA2-26WYT-Q2BKE";
        } else {
          getAddressUrl = "https://apis.map.qq.com/ws/geocoder/v1/?location=" + options.latitude + "," + options.longitude + "&key=OTXBZ-RNX3P-VEIDG-VJDA2-26WYT-Q2BKE";
        }
       
        wx.request({
          url: getAddressUrl,
          success: function (result) {
            console.log(result.data.result)
            that.setData({ address: result.data.result.address });
          }
        })
        if (options.latitude == undefined || options.latitude == ""){
          that.setData({
            latitude: res.latitude,
            longitude: res.longitude,
          });
        }else{
          that.setData({
            latitude: options.latitude,
            longitude: options.longitude,
          });
        }
        
        that.getDustbin(res.longitude, res.latitude, that.data.start, that.data.size);
      },
      
    })
  },
  getDustbin(lng, lat, _start, _size) {
    var _markets = [];
    var data = {
      id: "",
      latitude: "",
      longitude: "",
      width: 38,
      height: 48,
      iconPath: "img/position2.png"
    };
    var _this = this
    httpApi.dustbin(lng, lat, _start, _size).then(res => {
      var arr = res.data.records;
      for(var i=0;i<arr.length;i++){
        data.id = i+1;
        if (arr[i].lat == null){
          data.latitude = "";
        }else{
          data.latitude = arr[i].lat;
        }
        if (arr[i].lng == null){
          data.longitude = "";
        }else{
          data.longitude = arr[i].lng;
        }
        _markets.push(data);
         data = {
          id: "",
          latitude: "",
          longitude: "",
          width: 38,
          height: 48,
          iconPath: "img/position2.png"
        };
      }
      _this.setData({
        start: _start,
        markers: _markets,
      })
    })
  },
})
